home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / time / sclock / source / timer.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  4KB  |  195 lines

  1. /****************************************************************************
  2. *
  3. * VERSION
  4. *    $VER: timer.c 1.72 (30.12.93)
  5. *
  6. * DESCRIPTION
  7. *    Handle code for clock timer...
  8. *
  9. * AUTHOR
  10. *    Rune Johnsrud
  11. *
  12. * COPYRIGHT
  13. *    (c) 1993 Amiga Freelancers
  14. *
  15. *****************************************************************************/
  16.  
  17. #define INTUI_V36_NAMES_ONLY
  18.  
  19. #include <intuition/intuition.h>
  20. #include <exec/types.h>
  21. #include <exec/io.h>
  22. #include <exec/memory.h>
  23. #include <devices/timer.h>
  24. #include <utility/date.h>
  25.  
  26. #include <clib/alib_stdio_protos.h>
  27.  
  28. #include <proto/intuition.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <proto/timer.h>
  32. #include <proto/utility.h>
  33.  
  34. #include "global.h"
  35.  
  36. /************************************************************************/
  37.  
  38. extern struct GlobalData *gd;
  39. extern struct ClockInfo  *ci;
  40. extern struct Library *TimerBase;
  41. extern UBYTE  *Months[];
  42. extern UBYTE  *Days[];
  43.  
  44. /************************************************************************/
  45.  
  46. UBYTE *DateFmtStr[] =
  47. {
  48.     "%02ld%s%s%s%04ld",
  49.     "%02ld%s%s%s%s",
  50.     "%02ld%s%02ld%s%04ld",
  51.     "%02ld%s%02ld%s%s",
  52. };
  53.  
  54. UBYTE *TimeFmtStr[] =
  55. {
  56.     "%02ld%s%02ld%s%02ld",
  57.     "%02ld%s%02ld",
  58. };
  59.  
  60. /************************************************************************/
  61.  
  62. UBYTE strbuff[10];
  63.  
  64. /************************************************************************/
  65.  
  66. static UBYTE *GetShortYear(UWORD year);
  67.  
  68. /************************************************************************/
  69.  
  70.  
  71. BOOL OpenTimerDevice(VOID)
  72. {
  73.     gd->TimerPort = CreateMsgPort();
  74.     if (!gd->TimerPort) return FALSE;
  75.  
  76.     gd->TReq = (struct timerequest *) CreateExtIO(gd->TimerPort, sizeof(struct timerequest));
  77.     if (!gd->TReq) return FALSE;
  78.  
  79.     if (!OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *) gd->TReq, 0))
  80.     {
  81.         TimerBase = (struct Library *) gd->TReq->tr_node.io_Device;
  82.  
  83.         gd->TReq->tr_node.io_Message.mn_ReplyPort = gd->TimerPort;
  84.         gd->TReq->tr_node.io_Command              = TR_ADDREQUEST;
  85.         gd->TReq->tr_node.io_Flags                = NULL;
  86.         gd->TReq->tr_node.io_Error                = NULL;
  87.         gd->TReq->tr_time.tv_secs                  = 1;
  88.         gd->TReq->tr_time.tv_micro                  = 0;
  89.  
  90.         SendIO((struct IORequest *) gd->TReq);
  91.     }
  92.     else
  93.         return FALSE;
  94.  
  95.     return TRUE;
  96. }
  97.  
  98.  
  99. VOID CloseTimerDevice(VOID)
  100. {
  101.     if (gd->TReq)
  102.     {
  103.         AbortIO((struct IORequest *) gd->TReq);
  104.         WaitIO((struct IORequest *) gd->TReq);
  105.         CloseDevice((struct IORequest *) gd->TReq);
  106.         DeleteExtIO((struct IORequest *) gd->TReq);
  107.     }
  108.  
  109.     if (gd->TimerPort) DeleteMsgPort(gd->TimerPort);
  110. }
  111.  
  112.  
  113. VOID GetCurrSysTime(VOID)
  114. {
  115.     static struct ClockData cd;
  116.  
  117.     GetSysTime(&gd->TVal);
  118.     Amiga2Date(gd->TVal.tv_secs, &cd);
  119.  
  120.     if (ci->ClockType == CT_ANALOG)
  121.     {
  122.         if (cd.min != gd->CData.min)
  123.         {
  124.             gd->NewTime = TRUE;
  125.             gd->NewMin  = TRUE;
  126.             gd->NewHour = TRUE;
  127.         }
  128.     }
  129.     else
  130.     {
  131.         if (ci->ShowDate)
  132.         {
  133.             if ((cd.mday  != gd->CData.mday ) ||
  134.                 (cd.month != gd->CData.month) ||
  135.                 (cd.year  != gd->CData.year ))
  136.             {
  137.                 switch (ci->DateFormat)
  138.                 {
  139.                 case 0:
  140.                     sprintf(gd->DateStr, DateFmtStr[0],
  141.                         cd.mday, ci->DateSep, Months[cd.month - 1], ci->DateSep, cd.year);
  142.                     break;
  143.                 case 1:
  144.                     sprintf(gd->DateStr, DateFmtStr[1],
  145.                         cd.mday, ci->DateSep, Months[cd.month - 1], ci->DateSep, GetShortYear(cd.year));
  146.                     break;
  147.                 case 2:
  148.                     sprintf(gd->DateStr, DateFmtStr[2],
  149.                         cd.mday, ci->DateSep, cd.month, ci->DateSep, cd.year);
  150.                     break;
  151.                 case 3:
  152.                     sprintf(gd->DateStr, DateFmtStr[3],
  153.                         cd.mday, ci->DateSep, cd.month, ci->DateSep, GetShortYear(cd.year));
  154.                     break;
  155.                 }    
  156.  
  157.                 gd->NewDate = TRUE;
  158.             }
  159.         }
  160.  
  161.         if (ci->ShowSec)
  162.         {
  163.             sprintf(gd->TimeStr, TimeFmtStr[0],
  164.                 cd.hour, ci->TimeSep, cd.min, ci->TimeSep, cd.sec);
  165.  
  166.                 gd->NewTime = TRUE;
  167.         }
  168.         else
  169.         {
  170.             if (cd.min != gd->CData.min)
  171.             {
  172.                 sprintf(gd->TimeStr, TimeFmtStr[1],
  173.                     cd.hour, ci->TimeSep, cd.min);
  174.  
  175.                     gd->NewTime = TRUE;
  176.             }
  177.         }
  178.     }
  179.  
  180.     gd->CData = cd;
  181. }
  182.  
  183.  
  184. static UBYTE *GetShortYear(UWORD year)
  185. {
  186.     UBYTE *p = (UBYTE *) &strbuff;
  187.  
  188.     sprintf(strbuff, "%04ld\0", year);
  189.  
  190.     return (p + 2);
  191. }
  192.  
  193.  
  194. /* End Of File */
  195.